// pages/orderControl/orderDetail/orderDetail.js
import {
  onlineOrderDetails,
  shippingCompany,
  goDelivery
} from '../../../api.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allOrderInfo: {}, //小程序订单详情
    index: 0, //该订单上一页的订单index
    index2: 0, //该订单在上一页的订单下的子订单index
    showToast: false,
    isShowDelivery: false, //物流公司列表显隐
    deliveryCompany: [], //快递公司
    shipping_name: '',
    goDelivery: {
      shipping_code: '', //物流公司
      shipping_sn: '', //物流公司单号
      batchcode: '', //商品号
      info_batchcode: '' //订单详情号
    },
    formID:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('接收deid', options)

    onlineOrderDetails({
      token: app.globalData.token,
      batchcode: options.info_batchcode
    }).then(res => {
      if(res.data.code==666){
        console.log(res)
        if (options.type == 'todo') {
          this.setData({
            fromIndex: true,
            allOrderInfo: res.data.data.redata,
          })
          console.log(this.allOrderInfo)
        } else {
          this.setData({
            allOrderInfo: res.data.data.redata,
            index: Number(options.index),
            index2: Number(options.index2)
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })

    shippingCompany({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        deliveryCompany: res.data.data
      })
    })

  },
  fuzhi(e) {
    let content = e.target.dataset.con
    wx.setClipboardData({
      data: content,
      success() {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  selectedShipping() {
    this.setData({
      isShowDelivery: true
    })
  },
  selectedCom(e) {
    this.setData({
      'goDelivery.shipping_code': e.currentTarget.dataset.id,
      shipping_name: e.currentTarget.dataset.company,
      isShowDelivery: false
    })
  },
  goDelivery(e) {
    this.setData({
      showToast: true,
      'goDelivery.batchcode': e.currentTarget.dataset.batchcode,
      'goDelivery.info_batchcode': e.currentTarget.dataset.info_batchcode
    })
    // 'goDelivery.info_batchcode': this.data.goDelivery.info_batchcode
  },
  shippingSn(e) {
    this.setData({
      'goDelivery.shipping_sn': e.detail.value
    })
  },
  cancelDelivery() {
    this.setData({
      showToast: false
    })
  },
  confirmDelivery() {
    var _this = this;
    let goDeliveryInfo = _this.data.goDelivery;
    goDeliveryInfo.token = app.globalData.token;
    goDelivery(goDeliveryInfo).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          mask: true,
        })
        if (_this.data.fromIndex) {

        } else {
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1]; //当前页面
          var prevPage = pages[pages.length - 2]; //上一个页面

          //修改订单的单个状态
          var status = 'onlineOrderList[' + _this.data.index + '].product_data[' + _this.data.index2 + '].status';
          var status_value = 'onlineOrderList[' + _this.data.index + '].product_data[' + _this.data.index2 + '].sales_value';
          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            [status]: 3,
            [status_value]: '待收货'
          })
          // 修改订单的总状态
          var total_status = 'onlineOrderList[' + _this.data.index + '].status';
          var total_status_value = 'onlineOrderList[' + _this.data.index + '].sales_value';
          var num = 0;
          var orderData = prevPage.data.onlineOrderList[_this.data.index];
          orderData.product_data.forEach(function(i) {
            if (i.status == 3) {
              num++
            }
          })
          if (num == orderData.product_data.length) {
            prevPage.setData({
              [total_status]: 3,
              [total_status_value]: '待收货'
            })
          } else {
          }
        }
        _this.setData({
          showToast: false,
          'allOrderInfo.sales_value': '待收货',
          'allOrderInfo.status': 3
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          mask: true,
        })
      }
    })
  },
  // 修改人:颜阁
  // 修改日期：2018.10.29
  // 修改内容：增加扫描系统
  scanning: function () {
    var that = this
    wx.showLoading({
      title: '正在扫描',
    })
    wx.scanCode({
      success(res) {
        wx.hideLoading()
        wx.showToast({
          title: '扫描成功',
        })
        that.data.goDelivery.shipping_sn = res.result
        that.setData({
          goDelivery: that.data.goDelivery,
          showToast: true
        })
      }, fail() {
        wx.hideLoading()
        wx.showToast({
          title: '扫描失败',
          icon: 'none'
        })
      }
    })
  }
})